package com.fdl.dao.model.security;

import java.io.Serializable;
import java.util.Date;

import com.fdl.dao.base.Dao;


public abstract class AbstractDomainObject implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3599688256498911919L;

	private Serializable id;

	protected abstract Dao getDao();

	private Date createDate;

	private Date updateDate;

	private Serializable createUser;

	private Serializable updateUser;

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Serializable getCreateUser() {
		return createUser;
	}

	public void setCreateUser(Serializable createUser) {
		this.createUser = createUser;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public Serializable getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(Serializable updateUser) {
		this.updateUser = updateUser;
	}

	public Serializable getId() {
		return id;
	}

	public void setId(Serializable id) {
		this.id = id;
	}

	protected void persist() {
		getDao().saveOrUpdate(this);
	}

	protected void create() {
		createDate = new Date();
		persist();
	}

	protected void update() {
		updateDate = new Date();
		persist();
	}

	/*
	 * delete
	 */
	public void save() {
		if (id == null)
			create();
		else
			update();
	}

	public void remove() {
		getDao().remove(this);
	}

	public void evict() {
		getDao().saveOrUpdate(this);
	}

	public boolean equals(Object obj) {
		if (obj == null)
			return false;

		if (!this.getClass().equals(obj.getClass()))
			return false;

		Serializable oid = ((AbstractDomainObject) obj).getId();

		return oid.equals(id);
	}

	public String toString() {
		return "[id:" + id + "]";
	}
}
